package com.bridgeintelligent.tag.webserver.dim.mgmt.api.export;

import com.alibaba.excel.annotation.ExcelProperty;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * @author JackCheung
 * @date 2020/7/9 11:18 AM.
 * description:标签数据模型sheet页
 */
@Data
public class ExcelDimDataModel {

    /**
     * 将excel中中文的时间周期转换为系统字段
     */
    private static Map<String, String> updateCycleMap = new HashMap<>(16);
    /**
     * 将excel中中文的标签类型转换为系统字段
     */
    private static Map<String, String> classifyMap = new HashMap<>(16);
    /**
     * 反转
     */
    public static Map<String, String> reversalUpdateCycleMap = new HashMap<>(16);
    /**
     * 反转
     */
    public static Map<String, String> reversalClassifyMap = new HashMap<>(16);
    /**
     * 将excel中中文的标签业务类型转换为系统字段
     */
    private static Map<String, String> businessMap = new HashMap<>(16);
    /**
     * 反转
     */
    public static Map<String, String> reversalBusinessMap = new HashMap<>(16);

    static {
        updateCycleMap.put("每日", "d");
        updateCycleMap.put("每周", "w");
        updateCycleMap.put("每月", "m");
        classifyMap.put("描述标签", "01");
        classifyMap.put("标志标签", "02");
        classifyMap.put("数值标签", "03");
        classifyMap.put("标识标签", "04");
        classifyMap.put("日期标签", "05");
        classifyMap.put("集合标签", "06");
        businessMap.put("事实标签", "01");
        businessMap.put("预测标签", "02");
        businessMap.put("规则标签", "03");
        businessMap.put("标签因子", "04");
        updateCycleMap.forEach((k, v) -> reversalUpdateCycleMap.put(v, k));
        classifyMap.forEach((k, v) -> reversalClassifyMap.put(v, k));
        businessMap.forEach((k, v) -> reversalBusinessMap.put(v, k));
    }

    /**
     * 存储目录ID
     */
    private String catalogId;
    /**
     * 一级分类
     */
    @ExcelProperty(index = 1)
    private String firstCatalog;
    /**
     * 二级分类->catalogId
     */
    @ExcelProperty(index = 2)
    private String secondCatalog;
    /**
     * 标签序号，业务说用来排序用？？？ ->sortNum
     */
    @ExcelProperty(index = 3)
    private int sortNum;
    /**
     * 标签ID->dimId
     */
    @ExcelProperty(index = 4)
    private String dimId;
    /**
     * 标签名称->dimId
     */
    @ExcelProperty(index = 5)
    private String dimName;
    /**
     * 标签类型->classify
     */
    @ExcelProperty(index = 6)
    private String classify;
    /**
     * 业务类型 01 事实标签、02 预测标签、03 模型标签、04 标签因子
     */
    @ExcelProperty(index = 7)
    private String businessType;
    /**
     * 业务含义->definition
     */
    @ExcelProperty(index = 8)
    private String definition;
    /**
     * 应用场景->scenarios
     */
    @ExcelProperty(index = 9)
    private String scenarios;
    /**
     * 业务规则和业务取值范围，对应数据库字段为产出办法output_method
     * ->
     */
    @ExcelProperty(index = 10)
    private String outputMethod;
    /**
     * 展示规则->showType
     */
    @ExcelProperty(index = 11)
    private String showType;
    /**
     * 是否现成标签 ->isTagFlag
     */
    @ExcelProperty(index = 12)
    private String isTagFlag;
    /**
     * 标签缺省值->defaultValue
     */
    @ExcelProperty(index = 13)
    private String defaultValue;
    /**
     * 标签所属机构号->orgNo
     */
    @ExcelProperty(index = 14)
    private String orgNo;
    /**
     * 标签来源类型->tagSourceType
     */
    @ExcelProperty(index = 15)
    private String tagSourceType;
    /**
     * 更新周期 更新周期:d表示每日更新,m表示每月更新,w表示每周更新->updataCycle
     */
    @ExcelProperty(index = 16)
    private String updateCycle;
    /**
     * 标签归属方 = *标签业务联系部门->belongTo
     */
    @ExcelProperty(index = 17, converter = DepartmentConverter.class )
    private String belongTo;
    /**
     * 业务负责人->businessLeader
     */
    @ExcelProperty(index = 18)
    private String businessLeader;
    /**
     * 生效日期->
     */
    @ExcelProperty(index = 19, converter = DateConverter.class )
    private String effectiveTime;
    /**
     * 更新日期->
     */
    @ExcelProperty(index = 20)
    private String uptTime;
    /**
     * 失效日期->
     */
    @ExcelProperty(index = 21)
    private String unEffective;
    /**
     * *来源 *标签归属方 = *标签业务联系部门->businessDepartment
     */
    @ExcelProperty(index = 22)
    private String businessDepartment;
    /**
     * 来源系统->dataSource
     */
    @ExcelProperty(index = 23)
    private String dataSource;
    /**
     * 数据库英文名->sourcesDb
     */
    @ExcelProperty(index = 24)
    private String sourcesDb;
    /**
     * 来源系统表英文->sourcesTable
     */
    @ExcelProperty(index = 25)
    private String sourcesTable;
    /**
     * 来源系统字段英文名->sourcesColumn
     */
    @ExcelProperty(index = 26)
    private String sourcesColumn;
    /**
     * 数据表客户类型->tagCustType
     */
    @ExcelProperty(index = 27)
    private String tagCustType;
    /**
     * 时间周期->timeCycle
     */
    @ExcelProperty(index = 28)
    private String timeCycle;

    /**
     * 所属类型->belongsType
     * 01 公共标签
     * 02 部门标签
     */
    @ExcelProperty(index = 29)
    private String belongsType;

    public DimAggData converToDimAgg(ExcelDimDataModel dimDataModel, ExcelDimItemModel itemModel) {
        DimAggData result = new DimAggData();
        setItemModel(result, itemModel);
        setDimData(dimDataModel, result);
        return result;
    }

    private void setDimData(ExcelDimDataModel dimDataModel, DimAggData result) {
        result.setBelongTo(dimDataModel.getBelongTo());
        result.setBusinessDepartment(dimDataModel.getBelongTo());
        result.setBusinessLeader(dimDataModel.getBusinessLeader());
        result.setCatalogId(dimDataModel.getCatalogId());
        result.setClassify(classifyMap.get(dimDataModel.getClassify()));
        result.setDataSource(dimDataModel.getDataSource());
        result.setDefaultValue(dimDataModel.getDefaultValue());
        result.setDimId(dimDataModel.getDimId());
        result.setDimName(dimDataModel.getDimName());
        result.setDefinition(dimDataModel.getDefinition());
        result.setEffectiveTime(formatExcelDate(dimDataModel.getEffectiveTime()));
        result.setIsTagFlag(dimDataModel.getIsTagFlag());
        result.setOrgNo(dimDataModel.getOrgNo());
        result.setOutputMethod(dimDataModel.getOutputMethod());
        result.setScenarios(dimDataModel.getScenarios());
        result.setShowType(dimDataModel.getShowType());
        result.setSortNum(dimDataModel.getSortNum());
        result.setSourcesColumn(dimDataModel.getSourcesColumn());
        result.setSourcesDb(dimDataModel.getSourcesDb());
        result.setSourcesTable(dimDataModel.getSourcesTable());
        result.setTagCustType(dimDataModel.getTagCustType());
        result.setTagSourceType(dimDataModel.getTagSourceType());
        result.setTimeCycle(dimDataModel.getTimeCycle());
        result.setUnEffective(dimDataModel.getUnEffective());
        result.setUpdataCycle(updateCycleMap.get(dimDataModel.getUpdateCycle()));
        if (StringUtils.isNotEmpty(dimDataModel.getUptTime())) {
            result.setUptTime(formatExcelDate(dimDataModel.getUptTime()));
        } else {
            result.setUptTime(formatExcelDate(dimDataModel.getEffectiveTime()));
        }
        result.setFirstCatalog(dimDataModel.getFirstCatalog());
        result.setSecondCatalog(dimDataModel.getSecondCatalog());
        result.setBusinessType(businessMap.get(dimDataModel.getBusinessType()));
    }

    private void setItemModel(DimAggData dimAggData, ExcelDimItemModel itemModel) {
        if (null != itemModel) {
            dimAggData.setItemName(itemModel.getItemName());
            dimAggData.setItemNo(itemModel.getItemNo());
        }
    }

    /**
     * 格式化Excel时间
     *
     * @param day 距离1900年的日期
     * @return yyyy-MM-dd
     */
    private String formatExcelDate(String day) {
        String[] split = day.split("/");
        StringBuffer buffer = new StringBuffer();
        buffer.append(split[0]);
        buffer.append("-");
        if (split[1].length() == 1){
            buffer.append("0");
            buffer.append(split[1]);
            buffer.append("-");
        }else{
            buffer.append(split[1]);
            buffer.append("-");
        }
        if (split[2].length() == 1){
            buffer.append("0");
            buffer.append(split[2]);
        }else{
            buffer.append(split[2]);
        }
        return buffer.toString();
    }

   /* private static String format(Date date) {
        return (new SimpleDateFormat("yyyy-MM-dd")).format(date);
    }

    private static Date addDay(Date date, int day) {
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(date);
        calendar.add(5, day);
        date = calendar.getTime();
        return date;
    }*/
}